# coding=utf-8
import pymysql
from common.caseLog import Log

class Connect_Mysql():
    def __init__(self, host, user, password, db_name):
        self.log = Log()
        try:
            self.db = pymysql.connect(host=host,
                                      user=user,
                                      password=password,
                                      db=db_name,
                                      charset='utf8mb4',
                                      cursorclass=pymysql.cursors.DictCursor
                                      )
        except pymysql.Error as e:
            self.log.error(e)

    def commit_table(self, sql):
        # 提交
        try:
            cursors = self.db.cursor()
            cursors.execute(sql)
            self.db.commit()
        except pymysql.Error as e:
            self.log.error(e)
        finally:
            self.db.close()


    def select_table(self, sql):
        # 查询
        try:
            cursors = self.db.cursor()
            cursors.execute(sql)
            result = cursors.fetchall()
            return result
        except pymysql.Error as e:
            self.log.error(e)
        finally:
            self.db.close()
